// pages/goods/survey/index.js
var app = getApp();
import * as echarts from '../../../ec-canvas/echarts';

function setChart(chart, rate, sumSuccess) {
    var option = {
        backgroundColor: "#ffffff",
        series: [{
            label: {
                normal: {
                    fontSize: 9,
                    show: true,
                    position: 'inside'
                }
            },
            clockwise: false,
            color: ['rgba(45, 152, 253, 1)', 'rgba(229, 243, 255, 1)'],
            type: 'pie',
            center: ['50%', '50%'],
            data: [{
                value: rate,
                name: rate + '%'
            }, {
                value: sumSuccess,
                name: sumSuccess + '%'
            }]
        }]
    };
    chart.setOption(option);
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '商品铺店概况',
            'color': true,
            'class': false
        },
        store: { //铺店概况饼图
            lazyLoad: true
        },
        navbarInitTop: 0, //导航栏初始化距顶部的距离
        isFixedTop: false, //是否固定顶部
        executorUserId: "",
        isNav: 5,
        pic: "https://pic.localhost.com/",
        isSearchList: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.oneComponent = this.selectComponent('#mychart-dom-bar1');
    },
    onShow: function () {
        var that = this;
        if (that.data.navbarInitTop == 0) {
            //获取节点距离顶部的距离
            wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
                if (rect && rect.top > 0) {
                    var navbarInitTop = parseInt(rect.top - 68);
                    that.setData({
                        navbarInitTop: navbarInitTop
                    });
                }
            }).exec();
        }
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    executorUserId: res.data.id,
                })
                that.init_one();
                that.one_pie();
                that.survey()
            }
        })
    },
    //初始化铺货饼图
    init_one: function (sumSuccess, rate) {
        this.oneComponent.init((canvas, width, height, dpr) => {
            const chart = echarts.init(canvas, null, {
                width: width,
                height: height,
                devicePixelRatio: dpr // new
            });
            setChart(chart, sumSuccess, rate)
            this.chart = chart;
            return chart;
        });
    },
    //铺店饼图获取添加数据
    one_pie: function () {
        var _this = this;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'workerBeeInfo/findWorkerShopOverview/',
            q: {
                executorUserId: _this.data.executorUserId
            }
        }), res => {
            var rate = [];
            var sumSuccess = [];
            _this.setData({
                countGoodsId: res.data[0].countGoodsId,
                countDealerId: res.data[0].countDealerId,
                waitForDealer: res.data[0].waitForDealer,
                succesDealer: res.data[0].succesDealer,
            })
            rate = Math.ceil(res.data[0].rate);
            sumSuccess = Math.floor(res.data[0].sumSuccess);
            _this.init_one(sumSuccess, rate)
        }, (res) => {})
    },
    survey() {
        var that = this,
        search = this.data.value;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'workerBeeInfo/findWorkerBeeStatusSum/',
            q: {
                goodsId: "",
                goodsName: search == undefined ? "" : search,
                status: that.data.isNav,
                executorUserId: that.data.executorUserId
            }
        }), res => {
            that.setData({
                goodsgoing: res.data[0].goodsGoing,
                successgoods: res.data[0].successGoods,
                goodsinfo: res.data[0].goodsInfo
            })
        }, (res) => {})
    },
    info: function (e) {
        wx.navigateTo({
            url: '/pages/goods/storeSurvey/details/index?goodsid=' + JSON.stringify(e.currentTarget.dataset.id) + "&goodsname=" + e.currentTarget.dataset.name + "&status=" + e.currentTarget.dataset.status
        })
    },
    handleTop(e) {
        this.setData({
            isNav: e.currentTarget.dataset.item
        })
        this.survey();
    },
    /* 监听页面滑动事件 */
    onPageScroll: function (e) {
        var that = this;
        var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
        //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
        var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
        //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
        if (that.data.isFixedTop === isSatisfy) {
            return false;
        }
        that.setData({
            isFixedTop: isSatisfy
        });
    },
    // 读取输入值
    // inputText: function (e) {
    //     this.setData({
    //         inputValue: e.detail.value
    //     })
    // },
    //跳转搜索
    search(e){
        wx.navigateTo({
            url: '/pages/goods/storeSurvey/search/index?status=' + e.currentTarget.dataset.status
        })
    }
})